// pages/initLock/initLock.js
import * as MSroomAPI from "../../api/interfaces/MSroom";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseImgs: [],
    checkType: 'IDCARD',
    orderNo: '', //	订单号-必传
    name: '', //	个人姓名-必传
    idCardNo: '', //	身份证号码-必传
    imageUrl: '', //	身份证正面照片信息-必选-当使用ID_CARD核验的时候传该参数
    imageBackUrl: '', //	身份证反面照片信息-必选-当使用ID_CARD核验的时候传该参数
    faceUrl: '',
    isRequesting: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    
    this.setData({
      orderNo: options.orderNo,
      name:options.name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  skip() {
    let data = {
      checkType: this.data.checkType,
      orderNo: this.data.orderNo,
      name: this.data.name,
      idCardNo: this.data.idCardNo,
      imageUrl: this.data.imageUrl,
      imageBackUrl: this.data.imageBackUrl,
      faceUrl: this.data.faceUrl,
    }
    console.log(data)
    if (data.name == '' || data.idCardNo == '') {
      wx.showToast({
        title: '请完善信息',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (data.checkType == 'FACE') {
      if (data.faceUrl == '') {
        wx.showToast({
          title: '请完善信息',
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    } else {
      if (data.imageUrl == '' || data.imageBackUrl == '') {
        wx.showToast({
          title: '请完善信息',
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    }
    if (this.data.isRequesting) return;
    
    this.setData({ isRequesting: true });
    MSroomAPI.hotelRealCheck(data).then(res => {
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 2000
      });
   
      if (res.code == 200) {
        wx.switchTab({
          url: "../order/orderList"
        })
        this.setData({ isRequesting: false });

  
      }else {
        this.setData({ isRequesting: false });
      }
    })
 
  },


 
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      checkType: e.detail.value,
      imageUrl:'',
      imageBackUrl:'',
      faceUrl:'',
    })

  },
  handleChooseImg(e) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (result) => {
        console.log(result);
        this.uploadFile(e.currentTarget.dataset.query, result.tempFilePaths[0])
      }
    })
  },
  uploadFile(text, v) {
    let url = 'https://lyms.imsdx.net/lyms/'
    wx.uploadFile({
      url: url + 'sys/common/upload',
      filePath: v,
      name: 'file',
      formData: {},
      success: (res) => {
        this.setData({
          [text]: url + JSON.parse(res.data).message
        })
      }
    })
  }
})